<?php
/**
 * Represents a validator.
 */
abstract class fily_Validator extends fily_Control {
    /**
     * control to validate
     * @var fily_Control
     */
    protected $controlToValidate;
    /**
     * the errormessage that will be displayed
     * @var string
     */
    protected $errorMessage;
    
    /**
     * the display style of the control
     * @var <type>
     */
    protected $displayStyle;

    /**
     * validation group  for javascript validators
     * @var <type>
     */
    protected $validationGroup;



    public function __construct() {
        parent::__construct('span');
        $this->setErrorMessage('*');
        $this->setDisplayStyle('inline');
        $this->getStyle()->set('display', 'none');
        $this->setExecutionTime(500);
    }

    /**
     * sets the control to validate
     * @param fily_Control $controlToValidate
     */
    public function setControlToValidate(fily_Control $controlToValidate) {
        $this->controlToValidate = $controlToValidate;
    }





    /**
     * returns the control to validate
     * @return <type>
     */
    public function getControlToValidate() {
        return $this->controlToValidate;
    }



    /**
     * sets the display style
     * @param <type> $displayStyle
     */
    public function setDisplayStyle($displayStyle) {
        $this->displayStyle = $displayStyle;

    }

    /**
     * returns the display style
     * @return string
     */
    public function getDisplayStyle() {
        return $this->displayStyle;
    }

    /**
     * sets the error message
     * @param string $value
     */
    public function setErrorMessage($value) {
        $this->errorMessage = $value;
    }

    /**
     * gets the error message
     * @return string
     */
    public function getErrorMessage() {
        return $this->errorMesssage;
    }


         /**
     * execution time for the javascript code
     * @var int
     */
    protected $executionTime;

    /**
     * get the execution time for the javascript code
     * @return <type>
     */
    public function getExecutionTime() {
        return $this->executionTime;
    }

    /**
     *  set the execution time for the javascript code
     * @param <type> $executionZone
     */
    public function setExecutionTime($executionTime) {
        $this->executionTime = $executionTime;
    }
    /**
     * sets the validation group
     * @param string $value
     */
    public function setValidationGroup($value) {
        $this->validationGroup = $value;
    }

    /**
     * returns the validation group
     * @return string
     */
    public function getValidationGroup() {
        return $this->validationGroup;
    }


        //overwrite
    protected function generateHtml() {
        $html = '<' . $this->getTag() . $this->attributes->toString() . ' >'. $this->errorMessage.'</'. $this->getTag() .'>';
        $this->prepared = false;
        return $html;
    }


   public function XMLAttributes(array $attributes, $innerValue) {

       foreach($attributes as $key => $value){
            switch ($key){
                case 'controltovalidate':
                    $this->lateBindingObjects[$key] = $value;
                    unset($attributes[$key]);
                    break;
                case 'errormesssage':
                    $this->setErrorMessage($value);
                     unset($attributes[$key]);
                    break;
               case 'validationgroup':
                    $this->setValidationGroup($value);
                     unset($attributes[$key]);
                    break;
                case 'displaystyle':
                    $this->setDisplayStyle($value);
                    unset($attributes[$key]);
                    break;
                case 'executiontime':
                    $this->setExecutionTime($value);
                    unset($attributes[$key]);
                    break;
            }
        }
        parent::XMLAttributes($attributes, $innerValue);
    }

   public function XMLLateBinding(fily_ControlToHtml $baseControl) {
       foreach($this->lateBindingObjects as $key => $value){
            switch ($key){
                case 'controltovalidate':
                    $control = fily_XMLConverter::findControl($value, $this, $baseControl);
                    
                    if($control!=null){
                        $this->setControlToValidate($control);
                    }
                     unset($this->lateBindingObjects[$key]);
                    break;
            }
         }
         parent::XMLLateBinding($baseControl);
   }

}
?>
